package cn.t.modules.scheduler.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 邮件发送
 * @author 陶敏麒
 * @date 2024/3/5 15:23
 */
@Component
@Slf4j
public class ScheduleMailSender {
    @Resource
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送简单邮件
     * @param to 收件人列表
     * @param cc 抄送人列表
     * @param subject 主题
     * @param text 内容
     */
    public void sendMail(Set<String> to, Set<String> cc, String subject, String text){
        SimpleMailMessage smm = new SimpleMailMessage();
        smm.setFrom(String.format("调度中心<%s>", this.from));//发送者
        if (CollectionUtils.isEmpty(to)) {
            log.warn("[邮件发送]未设置收件人");
            return;
        }
        // 收件人
        smm.setTo(to.toArray(new String[0]));
        if (!CollectionUtils.isEmpty(cc)) {
            smm.setCc(cc.toArray(new String[0]));
        }
        smm.setSubject(subject);//邮件主题
        smm.setText(text);//邮件内容
        this.javaMailSender.send(smm);//发送邮件
    }
}
